﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using Xajhzwb.Components.Data;
using System.Reflection;
using Xajhzwb.Components.Util;
using Xajhzwb.Components.Helper;

namespace Xajhzwb
{
    public partial class FormConfig : Form
    {
        public FormConfig()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Form Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormConfig_Load(object sender, EventArgs e)
        {
            try
            {
                Config config = new Config();

                // System
                chkIsAuto.Checked = config.IsAuto;
                chkIsLoginRun.Checked = config.IsLoginRun;
                chkIsMini.Checked = config.IsMini;
                chkAutoUpdate.Checked = config.IsAutoUpdate;
                txtDelayTime.Text = config.DelayTime.ToString();

                // NewWork
                chkIsProxy.Checked = config.IsProxy;
                chkIsProxy_CheckedChanged(sender, e);

                txtProxyServer.Text = config.ProxyServer;
                txtProxyPort.Text = config.ProxyPort.ToString();
                txtProxyUser.Text = config.ProxyUser;
                txtProxyPass.Text = config.ProxyPass;
                txtReadTimeOut.Text = config.ReadTimeout.ToString();
                
                // Others
                txtUserAgent.Text = config.UserAgent;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// confirm button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            btnApply_Click(sender, e);

            this.Close();
        }

        /// <summary>
        /// cancel button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// apply button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnApply_Click(object sender, EventArgs e)
        {
            try
            {
                Config config = new Config();

                // System
                config.IsAuto = chkIsAuto.Checked;
                config.IsLoginRun = chkIsLoginRun.Checked;
                config.IsMini = chkIsMini.Checked;
                config.IsAutoUpdate = chkAutoUpdate.Checked;
                config.DelayTime = int.Parse(txtDelayTime.Text);

                // NetWork
                config.IsProxy = chkIsProxy.Checked;
                config.ProxyServer = txtProxyServer.Text;
                config.ProxyPort = txtProxyPort.Text;
                config.ProxyUser = txtProxyUser.Text;
                config.ProxyPass = txtProxyPass.Text;
                config.ReadTimeout = int.Parse(txtReadTimeOut.Text);

                // Others
                config.UserAgent = txtUserAgent.Text;
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// get useragent
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGetUserAgent_Click(object sender, EventArgs e)
        {
            txtUserAgent.Text = WebHelper.GetDefaultUserAgent();
        }

        private void chkIsProxy_CheckedChanged(object sender, EventArgs e)
        {
            if (chkIsProxy.Checked)
            {
                txtProxyServer.Enabled = true;
                txtProxyPort.Enabled = true;
                txtProxyUser.Enabled = true;
                txtProxyPass.Enabled = true;
            }
            else
            {
                txtProxyServer.Enabled = false;
                txtProxyPort.Enabled = false;
                txtProxyUser.Enabled = false;
                txtProxyPass.Enabled = false;
            }
        }


    
    }
}
